package com.xkdx.guangguang.sql;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class NotifyDataHelper {
	public static String TAG = "NotifyDataHelper";
	private int version = 5;
	private SQLiteDatabase db;
	private NotifySQLiteHelper dbHelper;
	private String TABNAME = "notify";
	public String NOTIFYID = "notifyID";
	public String MESSAGE = "Message";
	public String ISREAD = "isRead";
	public String DATA = "data";
	public String OBJECTTYPE = "objectType";
	public String OBJECTVALUE = "objectValue";
	public String RETURNOBJECTTYPE = "returnObjectType";
	public String RETURNOBJECTVALUE = "ReturnObjectValue";

	public NotifyDataHelper(Context context) {
		dbHelper = new NotifySQLiteHelper(context, TABNAME, null, version);
	}

	public void Close() {
		if (db != null) {
			db.close();
			dbHelper.close();
		}
	}

	public void insertNotify(PushInfo pushInfo) {
		try {
			db = dbHelper.getReadableDatabase();
			if (pushInfo == null || db == null) {
				return;
			}
			Calendar calendar = GregorianCalendar.getInstance();
			calendar.setTimeInMillis(System.currentTimeMillis());

			int month = calendar.get(Calendar.MONTH) + 1;
			int day = calendar.get(Calendar.DAY_OF_MONTH);
			ContentValues values = new ContentValues();
			values.put(ISREAD, "false");
			values.put(DATA, day + "/" + month);
			values.put(MESSAGE, pushInfo.getMessage());
			values.put(OBJECTTYPE, pushInfo.getObjectType());
			values.put(OBJECTVALUE, pushInfo.getObjectValue());
			values.put(RETURNOBJECTTYPE, pushInfo.getReturnObjectType());
			values.put(RETURNOBJECTVALUE, pushInfo.getReturnObjectValue());
			long rowID = db.insert(TABNAME, null, values);
			//Log.i(TAG, "rowID-->" + rowID);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void deletNotify(String message) {
		db = dbHelper.getWritableDatabase();

		if (message == null || db == null) {
			return;
		}
		try {
			db.delete(TABNAME, "Message=?", new String[] { message });

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void upDateNotifyToisRead(PushInfo pushInfo) {
		try {
			db = dbHelper.getWritableDatabase();
			if (db == null || pushInfo == null) {
				return;
			}

			ContentValues values = new ContentValues();
			values.put(ISREAD, "true");
			db.update(TABNAME, values, "Message=?", new String[] { pushInfo.getMessage() });

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public List<PushInfo> getAllPushInfo() {
		List<PushInfo> list = new ArrayList<PushInfo>();
		try {
			db = dbHelper.getReadableDatabase();
			if (db == null) {
				return list;
			}
			Cursor cursor = db.query(TABNAME, null, null, null, null, null, null);
			while (cursor.moveToNext()) {
				PushInfo pushInfo = new PushInfo();
				pushInfo.setIsRead(cursor.getString(cursor.getColumnIndex(ISREAD)));
				pushInfo.setData(cursor.getString(cursor.getColumnIndex(DATA)));
				pushInfo.setMessage(cursor.getString(cursor.getColumnIndex(MESSAGE)));
				pushInfo.setObjectType(cursor.getString(cursor.getColumnIndex(OBJECTTYPE)));
				pushInfo.setObjectValue(cursor.getString(cursor.getColumnIndex(OBJECTVALUE)));
				pushInfo.setReturnObjectType(cursor.getString(cursor.getColumnIndex(RETURNOBJECTTYPE)));
				pushInfo.setReturnObjectValue(cursor.getString(cursor.getColumnIndex(RETURNOBJECTVALUE)));
				list.add(pushInfo);
				//Log.i(TAG, "pushInfo.isReag-->" + pushInfo.getIsRead());
			}
			cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	

	public class PushInfo {
		String isRead;
		String data;
		String status;
		String Message;
		String ObjectType;
		String ObjectValue;
		String ReturnObjectType;
		String ReturnObjectValue;

		public String getStatus() {
			return status;
		}

		public void setStatus(String status) {
			this.status = status;
		}

		public String getMessage() {
			return Message;
		}

		public void setMessage(String message) {
			Message = message;
		}

		public String getObjectType() {
			return ObjectType;
		}

		public void setObjectType(String objectType) {
			ObjectType = objectType;
		}

		public String getObjectValue() {
			return ObjectValue;
		}

		public void setObjectValue(String objectValue) {
			ObjectValue = objectValue;
		}

		public String getReturnObjectType() {
			return ReturnObjectType;
		}

		public void setReturnObjectType(String returnObjectType) {
			ReturnObjectType = returnObjectType;
		}

		public String getReturnObjectValue() {
			return ReturnObjectValue;
		}

		public void setReturnObjectValue(String returnObjectValue) {
			ReturnObjectValue = returnObjectValue;
		}

		public String getIsRead() {
			return isRead;
		}

		public void setIsRead(String isRead) {
			this.isRead = isRead;
		}

		public String getData() {
			return data;
		}

		public void setData(String data) {
			this.data = data;
		}

	}


}
